package com.qhgrain.app.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.paas.base.modules.utils.lang.StringUtil;
import com.paas.base.modules.utils.page.PageBean;
import com.pubframework.common.http.HttpUtil;
import com.qhgrain.app.constant.UrlEnum;
import com.qhgrain.app.service.SyncTo4AService;
import com.qhgrain.app.service.SynclogService;
import com.qhgrain.app.syncto4Ainfo.SynclogAppInfo;
import com.qhgrain.app.syncto4Ainfo.SynclogRoleInfo;
import com.qhgrain.app.syncto4Ainfo.SynclogUnitInfo;
import com.qhgrain.app.syncto4Ainfo.SynclogUserInfo;
import com.qhgrain.app.util.ConfigUtil;
import com.qhgrain.app.vo.SynclogVo;

@Controller
@RequestMapping("/synclog")
public class SynclogController {
	private static Logger LOG = LoggerFactory.getLogger(SynclogController.class);
	private static String serviceUrl = ConfigUtil.getProperties(UrlEnum.SYNCLOG)+"/synclog/1.0.0";
	@Autowired
	private SynclogService synclogService;
	@Autowired
	private SyncTo4AService syncTo4AService;
	
	@RequestMapping("/list")
	public String list(){
		return "synclog/synclog_list";
	}
	
	@RequestMapping("/list/table")
	public String querySynclogForTable(Model model,String dataName, String dataType, String operationType, String syncResult, String pageNo, String pageSize){
		
		PageBean<SynclogVo> pageBean = this.synclogService.queryPage(dataName,dataType,operationType, syncResult, pageNo, pageSize);
		model.addAttribute(pageBean);
		return "synclog/synclog_list_table";
	}
	@RequestMapping("/delete")
	@ResponseBody
	public JSONObject deleteSynclogByIds(String ids){
		
		JSONObject result = new JSONObject();
		if(StringUtil.isBlank(ids)){
			result.put("code", "0");
			result.put("msg", "id不能为空");
		}else{
			Map<String,Object> paramMap = new HashMap<String,Object>();
			paramMap.put("synclogIds", ids);
			
			try {
				String resStr = HttpUtil.post(serviceUrl+"/synclog/deleteById", paramMap);
				JSONObject resJson = JSONObject.parseObject(resStr);
				
				if("1".equals(resJson.getString("state"))){
					result.put("code", "success");
					result.put("msg", "成功");
				}else{
					result.put("code", "fail");
					result.put("msg", "系统出错，删除失败");
					LOG.error(resStr);
				}
			} catch (Exception e) {
				LOG.error(e.getMessage());
			}
		}
		return result;
	}
	
	@RequestMapping("/resyncFailData")
	@ResponseBody
	public JSONObject resyncFailData(){
		JSONObject result = new JSONObject();
		List<SynclogRoleInfo> syncRoles = new ArrayList<SynclogRoleInfo>();
		List<SynclogAppInfo> syncApps = new ArrayList<SynclogAppInfo>();
		List<SynclogUserInfo> syncUsers = new ArrayList<SynclogUserInfo>();
		List<SynclogUnitInfo> syncUnits = new ArrayList<SynclogUnitInfo>();
		
		try {
			this.synclogService.getResyncData(syncRoles, syncApps, syncUsers, syncUnits);
			
			this.syncTo4AService.resyncFailData(syncRoles,syncApps,syncUsers,syncUnits);
		
			result.put("code", "success");
		} catch (Exception e) {
			result.put("code", "fail");
			LOG.error(e.getMessage());
		}
		
		
		
		/*String url = serviceUrl + "/synclog/getResynclogRole";
		try {
			String vo = HttpUtil.post(url, new HashMap<String,Object>());
			JSONObject voJson = JSONObject.parseObject(vo);
			List<SynclogRoleInfo> syncRoles = JSONArray.parseArray(voJson.getString("syncRoles"), SynclogRoleInfo.class);
			if(0 < syncRoles.size()){
				for(SynclogRoleInfo synclogRole:syncRoles){
					if("add".equals(synclogRole.getSyncOpertaionType())){
						//this.syncTo4AService.syncAddRole(roleId, roleName, roleCode, roleType);
					}else if("delete".equals(synclogRole.getSyncOpertaionType())){
						//this.syncTo4AService.syncDelRole(roles);
					}
				}
			}
		
		} catch (IOException e) {
			result.put("code", "fail");
			LOG.error(e.getMessage());
		}*/
		return result;
	}
}
